from sqlalchemy import Index, String, DateTime, Date, Integer, DECIMAL, Column, UniqueConstraint
from config.database import Base
from sqlalchemy.orm import declared_attr

class KlineBase(Base):
    """
    股票k线父类
    """
    __abstract__ = True
    @declared_attr.directive
    def __table_args__(cls):
        return (
            UniqueConstraint("symbol", "trade_date", name=f"{cls.__tablename__}_symbol_tradedate"),
            Index(f'{cls.__tablename__}_symbol', cls.symbol),
            Index(f'{cls.__tablename__}_trade_date', cls.trade_date)
        )

    id = Column(Integer, primary_key=True, autoincrement=True, nullable=False, comment='id')
    trade_date = Column(Date, nullable=False, comment='交易日期')
    symbol = Column(String(20), nullable=False, comment='股票代码')
    open = Column(DECIMAL, nullable=True, comment='开盘价')
    close = Column(DECIMAL, nullable=True, comment='收盘价')
    high = Column(DECIMAL, nullable=True, comment='最高价')
    low = Column(DECIMAL, nullable=True, comment='最低价')
    vol = Column(DECIMAL, nullable=True, comment='成交量')
    amount = Column(DECIMAL, nullable=True, comment='成交额')
    amplitude = Column(DECIMAL, nullable=True, comment='振幅')
    pct_chg = Column(DECIMAL, nullable=True, comment='涨跌幅')
    chg = Column(DECIMAL, nullable=True, comment='涨跌额')
    turn = Column(DECIMAL, nullable=True, comment='换手率')
    create_time = Column(DateTime, nullable=False, comment='创建时间')
    update_time = Column(DateTime, nullable=False, comment='更新时间')
